/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */
// Import("ocr.js")
// Import("utils.js")
Import("ggUtils_今日头条.js")


var APP_NAME_JRTT = '今日头条'
var PACK_NAME_JRTT = 'com.ss.android.article.news'

var MAIN_PAGE_FLAG_JRTT = '头条'
var ENTER_CENTER_PAGE_FLAG_JRTT = '任务'
var CENTER_PAGE_FLAG_JRTT = '现金收益'

function skip_主界面的弹窗_JRTT() {
    //添加
    var detects = opencv.findImagesEx('图色195094.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    //x
    var detects = opencv.findImagesEx('图色413193.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    agentOrHidClick(ocrTextMoreInRegionComplete(0, 0.5, 1, 1, '取消', '好的'))
}

function skip_任务中心的弹窗_JRTT() {

    var detects = opencv.findImagesEx('图色413193.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    var allres = ocrAllRes()
    if (ocrTextMoreByRes(allres, '添加到', '常用功能')) {
        agentOrHidClick(ocrTextMoreInRegionComplete(0, 0.5, 1, 1, '取消', '好的'))
    }

    if (ocrTextMoreByRes(allres, '今日预约', '立即预约')) {
        var detects = opencv.findImagesEx('图色413193.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
        }
    }

    if (ocrTextMoreByRes(allres, '获得惊喜奖励', '恭喜获得', '领取惊喜奖励')) {
        var detects = opencv.findImagesEx('图色413193.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
        }
    }
}

function back2center_JRTT() {
    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领'))) {
        enter_任务中心_JRTT(true)
    }
}

// 启动任务
function start_app_JRTT() {
    // app.closeApp(PACK_NAME_JRTT)
    // sleep.millisecond(200)
    // printinfoOld('start_app_JRTT')
    log('启动今日头条')
    if (!startApp(PACK_NAME_JRTT)) {
        return false
    }

    // 跳过首页广告
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, MAIN_PAGE_FLAG_JRTT, '推荐', '关注', '任务', '商城')
    }, (times) => {
        skip_主界面的弹窗_JRTT()

        if (times > 2) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
    }, null, 3)

    return true
}

// 进入任务中心
function enter_任务中心_JRTT(iskip) {
    log('进入任务中心')

    if (iskip == undefined) iskip = false

    // 进入任务中心
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, ENTER_CENTER_PAGE_FLAG_JRTT)
    }, (times) => {
        skip_主界面的弹窗_JRTT()

        sleep.millisecond(500)
        SystemPrint.warn('未找到主界面期望标志位')
    }, (findView, stepTimes) => {
        if (stepTimes == 1) {
            skip_主界面的弹窗_JRTT()
        }
        agentOrHidClick(findView)
    }, () => {
        // 失败后的方案 或者其它方案进入
        clickOffectY(0.5, 1, 60)
    }, 3)


    // if (!iskip) {
    //     // 期望标志位
    //     expectView(() => {
    //         return ocrText(CENTER_PAGE_FLAG_JRTT, '签到必得', '签到领', '连续签到')
    //     }, (times) => {
    //         skip_任务中心的弹窗_JRTT()

    //         if (times > 5) {
    //             agentOrHidback()
    //             sleep.millisecond(500)
    //             // todo 未找到后 返回怎么做
    //         }
    //         sleep.millisecond(500)
    //         SystemPrint.warn('未找到任务界面期望标志位')
    //     }, (findView) => {
    //         // agentOrHidClick(findView)
    //     }, null, 6)
    // }
}


function open_签到_JRTT() {
    if (!isRunAppTask('taskjrtt', '签到')) {
        return
    }

    if (hasExcuteFunc('open_签到_JRTT') || getExcuteFuncTimes('open_签到_JRTT') > 2) {
        return
    }

    log('签到')

    if (getExcuteFuncTimes('open_签到_JRTT') <= 1) {
        agentOrHidClick(ocrText('签到领'), 2, () => {
            setExcuteFunc('open_签到_JRTT')

            // var flag = ocrText('看视频再')
            // if(getDectNumber(flag))
            watchGG_今日头条独享(ocrText('看视频再'))
        })
        watchGG_今日头条独享(ocrText('额外再'))
        agentOrHidClick(ocrText('好的', '开心收下'))
    }

    addExcuteFuncTimes('open_签到_JRTT')

    skip_任务中心的弹窗_JRTT()
}

function open_开宝箱_JRTT() {
    if (!isRunAppTask('taskjrtt', '开宝箱')) {
        return
    }
    printinfoOld('open_开宝箱_JRTT-------------------------------')
    skip_任务中心的弹窗_JRTT()

    agentOrHidClick(ocrText('开宝箱得金', '宝箱得金币'), 2, () => {
        watchGG_多次看广告(ocrTextMoreInRegion(0, 0.5, 1, 0.8, '看视频', '看广告'))
    })
}

function watch_看广告_JRTT() {
    if (!isRunAppTask('taskjrtt', '看视频')) {
        return
    }
    printinfoOld('watch_看广告_JRTT-------------------------------')
    skip_任务中心的弹窗_JRTT()

    if (hasExcuteFunc('watch_看广告_JRTT')) {
        return
    }
}

// 进行浏览任务
function start_浏览任务_JRTT() {
    if (!start_app_JRTT()) {
        return false
    }

    skip_主界面的弹窗_JRTT()

    //搜索icon
    //找色返回目标数组
    var detects = opencv.findMultiColorEx('图色837054.cv');
    if (detects != null) {
        agentOrHidClickAbsoultX(detects[0], screen.percentToWidth(0.5))
    } else {

        var detectsssicon = opencv.findImagesEx('图色616907.cv');
        if (detectsssicon != null) {
            agentOrHidClickAbsoultX(detectsssicon[0], screen.percentToWidth(0.5))
        }
    }

    enter60分钟视频(getExcuteFuncTimes('start_浏览任务_JRTT'))
    skip_主界面的弹窗_JRTT()

    // ocrText('羊毛君分享师', true)
    agentOrHidClickOffectY(ocrWaitForMoreTextMillisecond('羊毛君分享师', '羊毛君', 10), 300)
    // 判断是否播放
    var detectsbf = opencv.findImagesEx('图色741102.cv');
    if (detectsbf == null) {
        agentOrHidClick(ocrText('次播放'))
    }
    // ocrText('羊毛君分享师', true)

    addExcuteFuncTimes('start_浏览任务_JRTT')
}

function start_浏览商城_JRTT() {
    log('准备前往浏览商城3分钟')
    sleep.second(2)

    skip_任务中心的弹窗_JRTT()
    //找图返回目标数组
    var detects = opencv.findImagesEx('图色666035.cv');
    if (detects == null) {
        var ocr1 = opencv.OCREx('图色751541.cv');
        if (ocr1 != null) {
            detects = ocr1.getAllDetect()
        }
    }

    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    var detects = opencv.findImagesEx('图色413193.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    log('准备浏览商品')
    whilestep((stepTimes, allTimes, canBreak) => {
        if (stepTimes % 2 == 0) {
            agentSwip(0.5, 0.5, 0.5, 0.4)
        }
        if (stepTimes % 3 == 0) {
            var detects = opencv.findImagesEx('图色413193.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
            }
        }
    }, 180)

    backUntilFindView(() => ocrText('首页', '任务'), 3)
}

function start_浏览简易_JRTT() {
    if (!isRunAppTask('taskjrtt', '活跃')) {
        return
    }
    log('准备前往推荐页浏览5分钟')
    sleep.second(2)

    skip_任务中心的弹窗_JRTT()

    clickOffectY(0.1, 1, 80)

    whilestep((stepTimes, allTimes, canBreak) => {
        if (stepTimes % 2 == 0) {
            agentSwipSmallDown()
        }
        if (stepTimes % 5 == 0) {
            agentOrHidClick(ocrTextMoreComplete('禁止', '取消'))
        }
        // ocrTextTrue('查看详情')
        // var allres = ocrAllRes()
        // if(ocrTextMoreByRes(allres, '原始', '征途', '三国', '灵兽大', '武林外', '热血江湖', '青云决', '蜀门', '指尖仙')){

        // }
        // agentOrHidClick(ocrText('查看详情'), 2, () => {
        // })

    }, 300)

    log('随机看个视频或文章')
    var ocr1播放 = opencv.OCREx('图色742320.cv');
    if (ocr1播放 != null) {
        agentOrHidClick(ocr1播放.getAllDetect()[0])
        sleep.second(60)
    } else {

    }

    backUntilFindView(() => ocrText('首页', '任务'), 3)
}

function checkCurrentJb() {
    if (isRunAppTask('taskjrtt', '单广')) {
        ocrTextMoreInRegion(0, 0.1, 1, 0.2, '金币')
    }
}


function watchorll() {

    var flag = ocrWaitForText('看视频再领', '看视频再得', '看视频再', '看视频领')

    if (getExcuteFuncTimes('watchorll') < 1) {
        watchGG_今日头条独享(flag)
    } else {
        if (getDectNumber(flag) < 2500 && getDectNumber(flag) > 1200) {
            skip_任务中心的弹窗_JRTT()
            log('今日头条低于2500需要去增加活跃或权重')
            sleep.second(2)

            start_浏览商城_JRTT()
            start_浏览简易_JRTT()

            enter_任务中心_JRTT(true)
            move2Top(2)
        } else {
            watchGG_今日头条独享(flag, false, 0)
        }
    }

    addExcuteFuncTimes('watchorll')
}

function start_今日头条刷单广_Task() {
    log('开始今日头条刷单广')
    while (true) {
        log('获得惊喜奖励')
        if (ocrWaitForText('获得惊喜奖励', '恭喜获得', '领取惊喜奖励')) {
            if (isRunAppTask('taskjrtt', '单广')) {
                watchorll()
            } else {
                watchorllmore()
            }
        }


        if (getExcuteFuncTimes('jrtt_开宝箱得tiems') < 20) {
            log('开宝箱得')
            var bxfalg = ocrText('开宝箱得', '宝箱得金币')
            agentOrHidClick(bxfalg, 1, () => {
                agentOrHidClick(bxfalg, 1)
                addExcuteFuncTimes('jrtt_开宝箱得tiems')
                if (isRunAppTask('taskjrtt', '单广')) {
                    watchorll()
                } else {
                    watchorllmore()
                }
            })
        }

        agentOrHidClick(ocrText('惊喜奖励', '后结束'), 2, () => {
            if (isRunAppTask('taskjrtt', '单广')) {
                watchorll()
            } else {
                watchorllmore()
            }
        })

        log('判断是否广告内')
        var detects播音 = opencv.findImagesEx('图色681128.cv');
        if (detects播音 != null || ocrText('领取成功')) {
            agentOrHidClick(ocrText('领取成功', '跳过'), 2, () => {
                agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
            })
        }

        // agentOrHidClick(ocrText('点击领取', '后结束'), 2, () => {
        //     watchorll()
        // })
        log('结束单次循环关闭弹窗')
        agentOrHidClick(ocrText('开心收下', '领取成功'))
        if (ocrText('首页将不再', '不再展示任务')) {
            agentOrHidClick(ocrText('取消'))
        }


        log('进入睡觉')
        agentOrHidClick(ocrWaitForText('睡觉赚', '觉赚金'), 2, () => {
            agentOrHidClick(ocrWaitForText('我要睡', '要睡了', 4))

            skip_任务中心的弹窗_JRTT()
            agentOrHidClickOffectY(ocrWaitForText('可领取'), 20, 2, () => {
                if (isRunAppTask('taskjrtt', '单广')) {
                    watchorll()
                } else {
                    watchorllmore()
                }
            })

            skip_任务中心的弹窗_JRTT()

            if (!ocrText('关闭本页', '做任务领', '现金收益')) {
                agentOrHidback()
            }
        })

        agentOrHidClick(ocrText('吃饭赚', '饭赚金'), 2, () => {
            if (isRunAppTask('taskjrtt', '单广')) {
                watchorll()
            } else {
                watchorllmore()
            }
            if (!ocrText('关闭本页', '做任务领', '现金收益')) {
                agentOrHidback()
            }
        })


        sleep.second(5)
    }
}


function watchorllmore() {

    var flag = ocrWaitForText('看视频再领', '看视频再得', '看视频再', '额外再', '看视频领')
    var jinbin = getDectNumber(flag)
    if (jinbin == 4500) {
        addExcuteFuncTimes('watchgglowtimes', 0)
        addExcuteFuncTimes('watchgglowertimes', 0)
    }

    if (jinbin < 2000 && jinbin > 1000) {
        addExcuteFuncTimes('watchgglowertimes')
        if (hasExcuteFuncInTime是否在一段时间内执行过('lljrtt') || getExcuteFuncTimes('watchgglowertimes') > 2) {
            setExcuteFuncInTime一段时间内不再执行('start_收集金币_JRTT', 3 * 60 * 60)
        }
    }

    if (jinbin < 3000 && jinbin > 900) {
        if (!hasExcuteFuncInTime是否在一段时间内执行过('lljrtt') && getExcuteFuncTimes('watchgglowtimes') > 2) {
            skip_任务中心的弹窗_JRTT()
            log('今日头条低于3000需要去增加活跃或权重')
            sleep.second(2)
            setExcuteFuncInTime一段时间内不再执行('lljrtt', 20 * 60)
            start_浏览商城_JRTT()
            start_浏览简易_JRTT()
        } else {
            watchGG_今日头条独享(flag)
        }
        addExcuteFuncTimes('watchgglowtimes')

    } else {
        if (jinbin >= 2000) {
            watchGG_今日头条独享(flag)
        }

    }

    skip_任务中心的弹窗_JRTT()
}


function start_今日头条刷多广_Task() {
    log('开始今日头条刷多广')
    while (true) {
        log('获得惊喜奖励')
        if (ocrWaitForText('获得惊喜奖励', '恭喜获得', '领取惊喜奖励', '额外再', 30)) {
            watchorllmore()
        }
        log('开宝箱得')
        agentOrHidClick(ocrText('开宝箱得', '宝箱得金币'), 2, () => {
            watchorllmore()
        })

        agentOrHidClick(ocrText('惊喜奖励', '后结束'), 2, () => {
            watchorllmore()
        })
        // agentOrHidClick(ocrText('点击领取', '后结束'), 2, () => {
        //     watchorll()
        // })

        agentOrHidClick(ocrText('开心收下', '领取成功'))
        if (ocrText('首页将不再', '不再展示任务')) {
            agentOrHidClick(ocrText('取消'))
        }


        agentOrHidClick(ocrText('睡觉赚', '觉赚金'), 2, () => {
            agentOrHidClick(ocrWaitForText('我要睡', '要睡了', 4))

            skip_任务中心的弹窗_JRTT()
            agentOrHidClickOffectY(ocrWaitForText('可领取'), 20, 2, () => {
                watchorllmore()
            })

            skip_任务中心的弹窗_JRTT()
            if (!ocrText('关闭本页', '做任务领', '现金收益')) {
                agentOrHidback()
            }
        })

        agentOrHidClick(ocrText('吃饭赚', '饭赚金'), 2, () => {
            watchorllmore()
            if (!ocrText('关闭本页', '做任务领', '现金收益')) {
                agentOrHidback()
            }
        })

        sleep.second(5)
    }
}


function start_收集金币_JRTT() {
    currentAppPackName = PACK_NAME_JRTT
    if (!HANGUP_APPNAMES.find(item => item == '今日头条')) {
        log('今日头条不刷金')
        return
    }
    //    closeApp(PACK_NAME_JRTT)
    if (!start_app_JRTT()) {
        return false
    }

    enter_任务中心_JRTT()

    open_签到_JRTT()

    watch_看广告_JRTT()

    // open_开宝箱_JRTT()

    if (ocrWaitForText('获得惊喜奖励', '恭喜获得', '领取惊喜奖励', 15)) {
        if (isRunAppTask('taskjrtt', '单广')) {
            watchorll()
        } else {
            watchorllmore()
        }
    }

    if (isRunAppTask('taskjrtt', '开宝箱')) {
        if (getExcuteFuncTimes('jrtt_开宝箱得tiems') < 15) {
            agentOrHidClick(ocrText('开宝箱得', '宝箱得金币'), 2, () => {
                addExcuteFuncTimes('jrtt_开宝箱得tiems')
                if (isRunAppTask('taskjrtt', '单广')) {
                    watchorll()
                } else {
                    watchorllmore()
                }
            })
        }
    }

    agentOrHidClick(ocrText('惊喜奖励', '后结束'), 2, () => {
        if (isRunAppTask('taskjrtt', '单广')) {
            watchorll()
        } else {
            watchorllmore()
        }
    })

    if (getExcuteFuncTimes('jrtt_开宝箱得tiems') >= 15) {
        if (isRunAppTask('taskjrtt', '气泡')) {
            agentOrHidClick(ocrTextMoreInRegion(0, 0, 0.3, 1, '点击领'), 2, () => {
                if (isRunAppTask('taskjrtt', '单广')) {
                    watchorll()
                } else {
                    watchorllmore()
                }
            })
            // agentOrHidClick(ocrText('点击领取', '后结束'), 2, () => {
            //     watchorll()
            // })
        }
    }

    if (getExcuteFuncTimes('jrtt_开宝箱得tiems') >= 3) {
        agentOrHidClick(ocrText('吃饭赚', '饭赚金'), 2, () => {
            if (isRunAppTask('taskjrtt', '单广')) {
                watchorll()
            } else {
                watchorllmore()
            }
            if (!ocrText('关闭本页', '做任务领', '现金收益')) {
                agentOrHidback()
            }
        })
    }

    if (isBetweenTime(20, 24)) {
        agentOrHidClick(ocrText('睡觉赚', '觉赚金'), 2, () => {
            agentOrHidClick(ocrWaitForText('我要睡', '要睡了', 4))

            skip_任务中心的弹窗_JRTT()
            agentOrHidClickOffectY(ocrWaitForText('可领取', 5), 20, 2, () => {
                if (isRunAppTask('taskjrtt', '单广')) {
                    watchorll()
                } else {
                    watchorllmore()
                }
            })

            skip_任务中心的弹窗_JRTT()
            if (!ocrText('关闭本页', '做任务领', '现金收益')) {
                agentOrHidback()
            }
        })
    }

    agentOrHidClick(ocrText('开心收下', '领取成功'))
    if (ocrText('首页将不再', '不再展示任务')) {
        agentOrHidClick(ocrText('取消'))
    }

    stopAllApp()
    // closeApp(PACK_NAME_JRTT)
}

